package com.gcsoftware.web.service;

import com.gcsoftware.utils.SysConst;
import com.gcsoftware.web.beans.T_GW_APPS;
import com.gcsoftware.web.beans.T_GW_APP_IPS;
import com.gcsoftware.web.beans.T_GW_CHANGES;
import com.gcsoftware.web.beans.T_GW_INTERFS;
import com.gcsoftware.web.mapper.T_GW_CHANGES_MAPPER;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qs on 2018/3/28.
 */
@Service
public class T_GW_CHANGES_SERVICE {
    @Autowired
    T_GW_CHANGES_MAPPER mapper;

    public List<T_GW_CHANGES> queryForList(String c_VERIFY_ID) {
        return mapper.queryForList(c_VERIFY_ID,null);
    }

    public int batchInsert(List<T_GW_CHANGES> changes) {
        try {
            for (T_GW_CHANGES c : changes) {
                mapper.insertChange(c);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public T_GW_APPS getAppFromChange(T_GW_APPS appInfo,List<T_GW_CHANGES> cgs ) {
        for (T_GW_CHANGES cg : cgs) {
            if (SysConst.CHANGE_TYPE_APP_INFO.equals(cg.getC_CHANGE_TYPE())) {
                if (cg.getC_CHANGE_FIELD().equals("c_APP_NAME")) {
                    appInfo.setC_APP_NAME(cg.getC_CHANGE_VALUE());
                }
                else if("c_APP_TYPE".equals(cg.getC_CHANGE_FIELD())) {
                    appInfo.setC_APP_TYPE(cg.getC_CHANGE_VALUE());
                } else if ("c_APP_REMARK".equals(cg.getC_CHANGE_FIELD())) {
                    appInfo.setC_APP_REMARK(cg.getC_CHANGE_VALUE());
                }
            }
        }
        return appInfo;
    }

    public List<T_GW_APP_IPS> getIpsFromChange(String c_APP_ID,List<T_GW_CHANGES> cgs) {
        List<T_GW_APP_IPS> ips = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(cgs)) {
            T_GW_APP_IPS ip;
            Map<Integer, T_GW_APP_IPS> map = new HashMap<>();
            for (T_GW_CHANGES cg : cgs) {
                if (SysConst.CHANGE_TYPE_APP_IP_ADD.equals(cg.getC_CHANGE_TYPE())) {
                    ip = map.get(cg.getN_REALM_NO());
                    if (null == ip) {
                        ip = new T_GW_APP_IPS();
                        ip.setC_APP_ID( c_APP_ID);
                    }
                    if ("c_APP_IP".equals(cg.getC_CHANGE_FIELD())) {
                        ip.setC_APP_IP(cg.getC_CHANGE_VALUE());
                    } else {
                        ip.setC_APPIP_REMARK(cg.getC_CHANGE_VALUE());
                    }

                    map.put(cg.getN_REALM_NO(), ip);
                }
            }
            if (null != map && map.size() > 0) {
                for (Map.Entry<Integer, T_GW_APP_IPS> entry : map.entrySet()) {
                    ips.add(entry.getValue());
                }
            }
        }
        return ips;
    }


    public T_GW_INTERFS getChangeIntefsForService(String c_VERIFY_ID) {
        List<T_GW_CHANGES> changes = mapper.queryForList(c_VERIFY_ID , SysConst.CHANGE_TYPE_INTERF_SERVICE);
        T_GW_INTERFS interf = null;
        if(CollectionUtils.isNotEmpty(changes)) {
            interf = new T_GW_INTERFS();
            for (T_GW_CHANGES chg : changes) {
                if ("c_INTERF_ENDPOINT".equals(chg.getC_CHANGE_FIELD()))
                    interf.setC_INTERF_ENDPOINT(chg.getC_CHANGE_VALUE());
                else if ("c_INTERF_METHOD_NAME".equals(chg.getC_CHANGE_FIELD())) {
                    interf.setC_INTERF_METHOD_NAME(chg.getC_CHANGE_VALUE());
                } else if ("c_INTERF_NAMEP".equals(chg.getC_CHANGE_FIELD())) {
                    interf.setC_INTERF_NAMEP(chg.getC_CHANGE_VALUE());
                }
            }
        }
        return  interf;
    }
}
